#include "tools.h"
#include "status.h"

// 清理屏幕
void clear_screen(void)
{
	system("clear");
}

// 任意键继续
void anykey_continue(void)
{
	printf("按任意键继续...\n");
	getchar();
}

// 清理输入缓冲区
void clear_stdin(void)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;
}

// 获取指令
char get_cmd(char start,char end)
{
	clear_stdin();

	printf("请输入指令:");
	while(true)
	{
		char val = getch();
		if(val >= start && val <= end)
		{
			printf("%c\n",val);
			return val;
		}
	}
}

//字符密码转换数字密码
int ch_pass_in(char* str)
{
	int pas=0;
	int len = strlen(str);
	for(int i=0;i<len;i++)
	{
		pas = pas*10+(str[i]-'0');
	}
	return pas;
}

//数字转换字符串
char* in_pass_ch(char* str,int pas)
{
	int len = 0;
	int pass = pas;
	for(int i=0;pass!=0;i++)
	{	
		pass = pass/10;
		len++;
	}
	for(int i=len-1;i>=0;i--)
	{
		str[i] = '0' + pas%10;
		pas = pas/10;
	}
	str[len] = '\0';
	return str;
}

//最后修改时间
char* file_time(time_t t,char* str)
{
	struct tm* it = localtime(&t);
	sprintf(str,"%2d月 %02d %02d:%02d",it->tm_mon+1,it->tm_mday,it->tm_hour,it->tm_min);
	return str;
}

// 获取密码
char* get_passwd(char* passwd,bool is_show,size_t size)
{
	if(NULL == passwd) return NULL;
	int count = 0;
	do{
		char val = getch();
		if(127 == val)
		{
			if(count > 0)
			{
				if(is_show)printf("\b \b");
				count--;
			}
			continue;
		}
		else if(10 == val)
		{
			break;
		}
		passwd[count++] = val;
		if(is_show);// printf("%c",val);
	}while(count < size -1);
	passwd[count] = '\0';
	printf("\n");
	return passwd;
}

